package cn.shujuhai.qtadmin.platform.pluging.logging;

import java.time.LocalDateTime;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import cn.shujuhai.qtadmin.platform.domain.SysLogging;
import cn.shujuhai.qtadmin.platform.kit.SecurityUtil;
import cn.shujuhai.qtadmin.platform.kit.ServletUtil;

/**
 * Describe: 日 志 异 步 工 厂
 * 
 * @author dch
 */
@Component
public class LoggingFactory {

    public static final String NO_LOGIN = "未登录用户";

    /**
     * 执 行 日 志 入 库 操 作
     */
    public void recordLog(@NotNull SysLogging sysLog) {
        sysLog.setOperateAddress(ServletUtil.getRemoteHost());
        sysLog.setMethod(ServletUtil.getRequestUri());
        sysLog.setCreateTime(LocalDateTime.now());
        sysLog.setRequestMethod(RequestMethod.valueOf(ServletUtil.getMethod()));
        sysLog.setOperateUrl(ServletUtil.getRequestUri());
        sysLog.setBrowser(ServletUtil.getBrowser());
        sysLog.setRequestBody(ServletUtil.getQueryParam());
        sysLog.setSystemOs(ServletUtil.getSystem());
        sysLog.setOperateName(null != SecurityUtil.currentUser()
            ? Objects.requireNonNull(SecurityUtil.currentUser()).getName() : NO_LOGIN);
        sysLog.insert();
    }
}
